<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui_ext/xmSelect/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title></title>
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
<%--树形显示--%>
    <div id="ID-tree-demo-showCheckbox"></div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">更新信息</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>
<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;
        var tree = layui.tree;
        // 模拟数据
        // var data = [{title:'早餐',id:1,children:[{title:'拌粉',id:5},{title:'蒸饺',id:6},{title:'豆浆',id:7}]},{title:'午餐',id:2,checked:true,children:[{title:'藜蒿炒腊肉',id:8},{title:'西湖醋鱼',id:9},{title:'小白菜',id:10},{title:'海带排骨汤',id:11}]},{title:'晚餐',id:3,children:[{title:'红烧肉',id:12,fixed:true},{title:'番茄炒蛋',id:13}]},{title:'夜宵',id:4,children:[{title:'小龙虾',id:14,checked:true},{title:'香辣蟹',id:15,disabled:true},{title:'烤鱿鱼',id:16}]}];
       var data = JSON.parse('${treeAllMenuList}')
        // 渲染
        tree.render({
            elem: '#ID-tree-demo-showCheckbox',
            data: data,
            showCheckbox: true,
            id:'test',
            edit: ['add', 'update', 'del'], // 开启节点的右侧操作图标
            customName: { // 自定义 data 字段名 --- 2.8.14+
                id: 'menuId',
                title: 'name',
                children: 'children'
            },
        });

        // 提交事件
        form.on('submit(demo1)', function(data){
            var checkData = tree.getChecked('test');
          // let menuIds = checkData.map(item=>item.menuId).join(",");

            let menuIds = "";
            for (let i = 0; i < checkData.length; i++) {
            //    获取一级菜单
                menuIds+=","+checkData[i].menuId;
            //    获取二级菜单
                let children = checkData[i].children;
                for (let j = 0; j < children.length; j++) {
                    menuIds+=","+children[j].menuId;
                }
            }
            menuIds = menuIds.substring(1);
            console.log("选择的菜单id:"+menuIds)
            var field = data.field;
            field.menuIds = menuIds;
            $.post("/role/add.do",field,function (res){
                if(res.code==0){
                    layer.msg(res.msg,{time:2000},function (){

                    })
                }else{
                    layer.msg(res.msg,{time:2000},function (){

                    })
                }
            })
            return false; // 阻止默认 form 跳转
        });

        // 日期
        laydate.render({
            elem: '#date'
        });

    });
</script>

</body>
</html>